﻿#include "MainWindow.h"
#include "ui_MainWindow.h"
int MainWindow:: LcdTimeNumber = 0;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{

    ui->setupUi(this);
    m_PushButton = ui->pushButton;
    m_time = new QTimer(this);
    //    m_time->start(100);
    connect(m_time,SIGNAL(timeout()),this,SLOT(dealTime()));
    connect(m_PushButton,&QPushButton::clicked,this,&MainWindow::on_start_clicked);
}



MainWindow::~MainWindow()
{
    delete ui;
}



void MainWindow::dealTime()
{

    LcdTimeNumber++;
    ui->lcdNumber->display(LcdTimeNumber);


}

void MainWindow::dealTime2()
{

    LcdTimeNumber++;
    ui->lcdNumber_2->display(LcdTimeNumber);


}



void MainWindow::on_start_clicked()
{
    if(m_time->isActive() == false)
    {
        m_time->start(100);
    }

    m_bIfClicked = !m_bIfClicked;
    LcdTimeNumber = 0;
    if(m_bIfClicked == true)
    {
        disconnect(m_time,SIGNAL(timeout()),this,SLOT(dealTime()));
        connect(m_time,SIGNAL(timeout()),this,SLOT(dealTime2()));
    }
    else
    {
        disconnect(m_time,SIGNAL(timeout()),this,SLOT(dealTime2()));
        connect(m_time,SIGNAL(timeout()),this,SLOT(dealTime()));
    }



    QThread:: sleep(1);//此时的QTread是主线程 当点击按钮,定时器虽然开启，但随即主线程又开启休眠，即主线程立刻休眠
    //当休眠完成后 开始执行定时器未完成的任务





    //    关闭定时器
    //    m_time->stop();
    //    qDebug()<<"over";
}
